<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/15
 * Time: 下午2:58
 */

namespace App\Sock\Controller;


use App\Utility\Db;
use EasySwoole\Core\Socket\Response;
use EasySwoole\Core\Socket\AbstractInterface\TcpController;
use EasySwoole\Core\Swoole\ServerManager;
use EasySwoole\Core\Swoole\Task\TaskManager;

class Device extends TcpController
{
    function actionNotFound(?string $actionName)
    {
        $this->response()->write("{$actionName} not found");
    }

    // 激光广电开关数据处理
    public function laser(){
        $params = $this->request()->getArg('data');
        $hard_id = $params['device'];
        $hard_status = $params['type'] == 2 ? 1 : 0;
        $time = $params['time'];
        // 判断入库条件，实际可以考虑将非默认值插入其中
        // 可以使用mysql连接池
        if ($hard_status) {
            $Db = (new Db())->dbConnector();
            $data = [
                'device_id' => $hard_id,
                'status'    => $hard_status,
                'time'      => $time
            ];
            // var_dump($data);

            $res = $Db->insert('data_switch', $data);
            echo 'res:' . $res . PHP_EOL;
            // $res = 1;
            // var_dump($res);
            if ($res) {
                $this->response()->write('R:OK');
            } else {
                $this->response()->write('R:Fail');
            }
        } else {
            $this->response()->write('R:Beat');
        }
    }

    // 未知的消息头
    public function error()
    {
        echo 'unknown message head' . PHP_EOL;
        $this->response()->write('unknown device');
    }


}